package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class fourteen {
    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> list=new ArrayList<>();
        HashMap<Character,Integer> hashMap=new HashMap<>();
        int n=p.length();
        int m=s.length();
        for(int i=0;i<n;i++){
            char in=p.charAt(i);
            hashMap.put(in, hashMap.getOrDefault(in,0)+1);
        }
        for(int j=0;j<=m-n;j++){
            HashMap<Character,Integer> hashMap1=new HashMap<>();
            for(int i=j;i<j+n;i++){
                char in1=s.charAt(i);
                hashMap1.put(in1, hashMap1.getOrDefault(in1,0)+1);
            }
            if(hashMap.equals(hashMap1)){
                list.add(j);
            }
        }
        return list;

    }

    public static void main(String[] args) {
//        s = "cbaebabacd", p = "abc"
        String s="abab";
        String p="ab";
        List<Integer> anagrams = findAnagrams(s, p);
        System.out.println(anagrams);
    }
}
